package HelperFinancial.DataBase;

import java.util.ArrayList;
import java.util.List;

import HelperFinancial.Class.Credito;
import HelperFinancial.Class.Debito;
import android.content.Context;
import android.database.Cursor;

public class CreditoAdapter {
	
	private CreditoHelper BDHelper;
	
	private static CreditoAdapter instance;
	
	private ConfigurationAdapter configuration;

	private CreditoAdapter(Context context) {
		this.BDHelper = new CreditoHelper(context);
		configuration  =  ConfigurationAdapter.getInstance(context);
	}
	
	public static CreditoAdapter getInstance(Context context){
		if(instance == null){
			instance = new CreditoAdapter(context);
		}
		
		return instance;
	}
	
	public static void destroy() {
		if(instance != null){
			instance.close();
		}
		instance = null;
	}
	
	private void close() {
		BDHelper.close();
	}

	public void addCredit(Credito credito){
		BDHelper.insertCredit(credito);
	}
	
	public void removeAllCredits(){
		BDHelper.removeAllCredits();
	}
	
	public void removeCredit(Integer Id) {
		BDHelper.removeCredit(Id);
	}
	
	public void removeFixa(int qntdCorrente, int idPai) {
		Cursor cr = BDHelper.getFilhosHabilitados(qntdCorrente, idPai);
		int colId = cr.getColumnIndex("_id");
		while (cr.moveToNext()) {
			removeCredit(cr.getInt(colId));
		}
	}
	
	public void updateCredit(Credito credito){
		BDHelper.updateCredit(credito);
	}
	
	public void updateFixa(Credito c, int qntdCorrente, int idPai) {
		Cursor cr = BDHelper.getFilhosHabilitados(qntdCorrente, idPai);
		int colId = cr.getColumnIndex("_id");
		int qCorrente = c.getQntdCorrente();
		int contador = -1;
		while (cr.moveToNext()) {
			contador++;
			c.setQntdCorrente(qCorrente);
			qCorrente++;
			c.setId(cr.getInt(colId));
			c.setDateDuration(incrementaMes(c.getDateDuration(), contador));
			updateCredit(c);
		}
	}
	
	private String incrementaMes(String data, int contador) {
		if (contador == 0) return data;
		int dia = Integer.parseInt(data.split("/")[0]);
    	int mes = Integer.parseInt(data.split("/")[1]) - 1;
    	int ano = Integer.parseInt(data.split("/")[2]);
    	
    	String retorno = "";
    	
    	for (int i = 0; i < contador; i++) {
			if (mes >= 11) {
				mes = 0;
				ano++;
				retorno = (formataData(dia, mes, ano));
			}
			else if ((mes + 1 == 3 || mes + 1 == 5 || mes + 1 == 8 || mes + 1 == 10) && (dia == 31)) {
				mes++;
				retorno = (formataData(30, mes, ano));
			}
			else if (mes + 1 == 1 && dia >= 28) {
				mes++;
				retorno = (formataData(28, mes, ano));
			}
			else {
				mes++;
				retorno = (formataData(dia, mes, ano));
			}
    	}
    	return retorno;
	}
	
    private String formataData(int dia, int mes, int ano) {
    	String data = "";
    	if(dia <= 9){
            data += "0" + dia + "/";
        }else{
            data += dia + "/";
        }
        if(mes <= 8){
            data += "0" + (mes+1) + "/";
        }else{
            data += (mes+1) + "/";
        }
        data += ano;
        return data;
    }
	
	public List<Credito> getAllCredits() {
		Cursor cr = BDHelper.getAllCredits();
		List<Credito> allCounts = new ArrayList<Credito>();
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		int colValue = cr.getColumnIndex("value");
		int colQntdMeses = cr.getColumnIndex("qntdMeses");
		int colDate = cr.getColumnIndex("date");
		
		
		while(cr.moveToNext()){
			Credito conta =  new Credito(
					cr.getInt(colId),
					cr.getString(colName),
					cr.getInt(colQntdMeses),
					cr.getDouble(colValue),
					cr.getString(colDate)
					);
			allCounts.add(conta);
		}
		return allCounts;
	}
	
	public Credito getCreditByName(String name){
		Cursor cr = BDHelper.getCreditByName(name);
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		int colValue = cr.getColumnIndex("value");
		int colQntdMeses = cr.getColumnIndex("qntdMeses");
		int colDate = cr.getColumnIndex("date");
		
		
		cr.moveToFirst();
		Credito credito =  new Credito(
				cr.getInt(colId),
				cr.getString(colName),
				cr.getInt(colQntdMeses),
				cr.getDouble(colValue),
				cr.getString(colDate)
		);
		
		return credito;
	}
	
	public Credito getCreditById(Integer id){
		Cursor cr = BDHelper.getCreditById(id);
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		int colValue = cr.getColumnIndex("value");
		int colQntdMeses = cr.getColumnIndex("qntdMeses");
		int colDate = cr.getColumnIndex("date");
		
		
		cr.moveToFirst();
		Credito credito =  new Credito(
				cr.getInt(colId),
				cr.getString(colName),
				cr.getInt(colQntdMeses),
				cr.getDouble(colValue),
				cr.getString(colDate)
		);
		credito.setQntdCorrente(cr.getInt(cr.getColumnIndex("corrente")));
		credito.setIdPai(cr.getInt(cr.getColumnIndex("pai")));
		
		return credito;
	}
	
	public double getCreditsSum(){
		//Cursor cr = BDHelper.getCreditsSum();
		Cursor cr = BDHelper.getCreditsByFilter(configuration.getDateInit());
		int colValue = cr.getColumnIndex("value");
		double value = 0;
		
		while (cr.moveToNext())
			value += cr.getDouble(colValue);
		
		return value;
	}
	
	public double getCreditsSumByDate(String date){
		Cursor cr = BDHelper.getCreditsSumByDate(date);
		int colValue = cr.getColumnIndex("valueTotal");
		
		cr.moveToFirst();
		double value = cr.getDouble(colValue);
		
		return value;
	}
	
	
	public List<Credito> getCreditsFilterByDate(String date) {
		Cursor cr = BDHelper.getCreditsByFilter(date);
		List<Credito> allCounts = new ArrayList<Credito>();
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		int colValue = cr.getColumnIndex("value");
		int colQntdMeses = cr.getColumnIndex("qntdMeses");
		int colDate = cr.getColumnIndex("date");
		
		
		while(cr.moveToNext()){
			Credito conta =  new Credito(
					cr.getInt(colId),
					cr.getString(colName),
					cr.getInt(colQntdMeses),
					cr.getDouble(colValue),
					cr.getString(colDate)
					);
			allCounts.add(conta);
		}
		return allCounts;
	}
	
	public List<Credito> getCreditsFilterByDate(String date, String type) {
		Cursor cr = BDHelper.getCreditsByFilter(date, type);
		List<Credito> allCounts = new ArrayList<Credito>();
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		int colValue = cr.getColumnIndex("value");
		int colQntdMeses = cr.getColumnIndex("qntdMeses");
		int colDate = cr.getColumnIndex("date");
		
		
		while(cr.moveToNext()){
			Credito conta =  new Credito(
					cr.getInt(colId),
					cr.getString(colName),
					cr.getInt(colQntdMeses),
					cr.getDouble(colValue),
					cr.getString(colDate)
					);
			allCounts.add(conta);
		}
		return allCounts;
	}
	
}
